package sqlsession.impl;/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2020-01-08 16:08
 * @notify
 * @version 1.0
 */

import executor.Executor;
import executor.impl.CachingExecutor;
import mapping.BoundSql;
import mapping.Configuration;
import mapping.MappedStatement;
import sqlsession.SqlSession;
import sqlsource.SqlSource;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

public class DefaultSqlSession implements SqlSession {
    private Configuration configuration;

    public DefaultSqlSession(Configuration configuration) {
        this.configuration = configuration;
    }

    @Override
    public <T> T selectOne(String statementId, Object param) throws Exception {
        List<Object> objects = selectList(statementId, param);
        if (objects.size() > 1) {
            throw new Exception("需要获取1个结果，但是获取到了" + objects.size() + "个结果");
        }
        return (T) objects.get(0);
    }

    @Override
    public <T> List<T> selectList(String statementId, Object param) throws Exception {
        Executor executor = configuration.newExecutor(statementId, param);
        Object query = executor.query(statementId, param);
        return (List<T>) query;
    }

    @Override
    public boolean insert(String statementId, Object param) throws Exception {
        Executor executor = configuration.newExecutor(statementId, param);
        return executor.insert();
    }
}
